package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CardService;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CardServiceImpl implements CardService {
    CartDao cartDao = new CartDaoImpl();
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Cart> squery(Cart cart) {
        List<Cart> list = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
           list = cartDao.query(cart);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public int sinsert(Cart cart) {
        int a = 0;
        try {
            conn = DBUtil.getConnection();
            DBUtil.beginTransaction();
            a = cartDao.insert(cart);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return a;
    }

    @Override
    public int supdatequa(Cart cart) {
        int a = 0;
        try {
            conn = DBUtil.getConnection();
            DBUtil.beginTransaction();
            a = cartDao.updatequa(cart);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return a;
    }

    @Override
    public int sdelete(Cart cart) {
        int a = 0;
        try {
            conn = DBUtil.getConnection();
            DBUtil.beginTransaction();
            a = cartDao.delete(cart);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return a;
    }
}
